﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;


namespace FingerVein_Database_Collection
{
    public partial class Load_User_Form : Form
    {
        FingerVeinDBModelContainer dbmodel = new FingerVeinDBModelContainer();

        public Load_User_Form()
        {
            InitializeComponent();

            UserManager usermanager = new UserManager();
            dgvLoadUser.DataSource = usermanager.GetUsers();
        }

        private void loadgrid()
        {
            dgvLoadUser.Rows.Clear();

            var userdta = from x in dbmodel.USER_TABLE
                       select x;

            int i = 0;
            foreach (var v in userdta)
            {
                i = dgvLoadUser.Rows.Add();
                dgvLoadUser.Rows[i].Cells["UserID"].Value = v.User_ID.ToString();
                dgvLoadUser.Rows[i].Cells["UserName"].Value = v.User_Name.ToString();
                dgvLoadUser.Rows[i].Cells["Gender"].Value = v.User_Gender;
                dgvLoadUser.Rows[i].Cells["Email"].Value = v.User_Email;
                dgvLoadUser.Rows[i].Cells["Age"].Value = v.User_Age.ToString();
                dgvLoadUser.Rows[i].Cells["Status"].Value = v.User_Status;
                dgvLoadUser.Rows[i].Cells["Nationality"].Value = v.User_Nationality;
                dgvLoadUser.Rows[i].Cells["Load"].Value = "Load";
                
            }
        }

        private void Load_User_Form_Load(object sender, EventArgs e)
        {
        
            
        }

        private void dgvLoadUser_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvLoadUser.Columns["Load"].Index && e.RowIndex >= 0)
            {
                int usrID = Convert.ToInt16(dgvLoadUser.Rows[e.RowIndex].Cells["UserID"].Value);

                var usrDt = from u in dbmodel.USER_TABLE
                            where u.User_ID == usrID
                            select u;

                var UsrFgr = from f in dbmodel.USER_FINGERS_TABLE
                             where f.User_ID == usrID
                             select f;

                MainForm.UserID = usrDt.FirstOrDefault().User_ID.ToString();
                MainForm.UserName = usrDt.FirstOrDefault().User_Name;
                MainForm.UserGender = usrDt.FirstOrDefault().User_Gender;
                MainForm.UserEmail = usrDt.FirstOrDefault().User_Email;
                MainForm.UserAge = usrDt.FirstOrDefault().User_Age.ToString();
                MainForm.UserStatus = usrDt.FirstOrDefault().User_Status;
                MainForm.UserNationality = usrDt.FirstOrDefault().User_Nationality;

                int i =0;
                foreach (var uf in UsrFgr)
                {
                    int fNoLI = 1;
                    int fNoLM = 1;
                    int fNoLR = 1;
                    int fNoRI = 1;
                    int fNoRM = 1;
                    int fNoRR = 1;

                    if (uf.User_Hand == "Left" && uf.Finger_Name == "Index" && uf.Finger_No == fNoLI.ToString())
                    {
                        MainForm.Left_Index[i] = ImageByteConv.ByteArrayToImage(uf.Finger_Data);
                        fNoLI++;
                    }
                    else if (uf.User_Hand == "Left" && uf.Finger_Name == "Middle" && uf.Finger_No == fNoLM.ToString())
                    {
                        MainForm.Left_Middle[i] = ImageByteConv.ByteArrayToImage(uf.Finger_Data);
                        fNoLM++;
                    }
                    else if (uf.User_Hand == "Left" && uf.Finger_Name == "Ring" && uf.Finger_No == fNoLR.ToString())
                    {
                        MainForm.Left_Ring[i] = ImageByteConv.ByteArrayToImage(uf.Finger_Data);
                        fNoLR++;
                    }
                   
                    else if (uf.User_Hand == "Right" && uf.Finger_Name == "Index" && uf.Finger_No == fNoRI.ToString())
                    {
                        MainForm.Right_Index[i] = ImageByteConv.ByteArrayToImage(uf.Finger_Data);
                        fNoRI++;
                    }
                    else if (uf.User_Hand == "Right" && uf.Finger_Name == "Middle" && uf.Finger_No == fNoRM.ToString())
                    {
                        MainForm.Right_Middle[i] = ImageByteConv.ByteArrayToImage(uf.Finger_Data);
                        fNoRM++;
                    }
                    else if (uf.User_Hand == "Right" && uf.Finger_Name == "Ring" && uf.Finger_No == fNoRR.ToString())
                    {
                        MainForm.Right_Ring[i] = ImageByteConv.ByteArrayToImage(uf.Finger_Data);
                        fNoRR++;
                    }

                }

            }
        }
    }
}
